package com.zxq.web.channel.platform;

import com.zxq.exchange.enums.EProtocol;
import com.zxq.exchange.enums.EFrame;
import com.zxq.exchange.model.ExchangeDTO;
import com.zxq.exchange.register.NotifyDeviceRegister;
import com.zxq.web.channel.SynchronismMessage;
import com.zxq.web.dto.PlatformCmdDTO;
import com.zxq.web.service.impl.ITransformServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;

@Service
@Slf4j
public class PlatformMessageServiceImpl implements IPlatformMessage {

    /**
     * 同步消息中心
     */
    @Autowired
    SynchronismMessage synchronismMessage;

    /**
     * 传输服务
     */
    @Autowired
    ITransformServiceImpl iTransformService;


    static PlatformMessageServiceImpl instance = null;

    @PostConstruct
    public void init() {
        instance = this;
    }

    public static PlatformMessageServiceImpl getInstance() {
        return instance;
    }


    @Override
    public PlatformCmdDTO platformCmd(PlatformCmdDTO platformCmdDTO) {
        PlatformCmdDTO result = null;
        EProtocol eProtocol = EProtocol.getType(platformCmdDTO.getProtocolId());
        if(eProtocol == EProtocol.UNKNOW) {
            log.info(String.format("devId:{0} not find protocol",platformCmdDTO.getDevId()));
            return null;
        }
        EFrame tcpFrame = EFrame.getType(platformCmdDTO.getFrameId());
        if(tcpFrame == EFrame.unknow){
            log.info(String.format("devId:{0} not find frame",platformCmdDTO.getDevId()));
            return null;
        }
        ExchangeDTO exchangeDTO = iTransformService.platTransform(platformCmdDTO);
        if(exchangeDTO == null) {
            log.info(String.format("devId:{0} not translate cmd",platformCmdDTO.getDevId()));
            return null;
        }
        switch (eProtocol) {
            case TCP:{
                NotifyDeviceRegister.getInstance(tcpFrame).notifyMessage(exchangeDTO);
                result =  synchronismMessage.waitCmdMessage(platformCmdDTO.getDevId(),platformCmdDTO.getCmdId());
                if(result == null) {
                    log.info(String.format("devId:{0} not response",platformCmdDTO.getDevId()));
                }
            }
            break;
        }

        return result;
    }


    @Override
    public void platformNotify(PlatformCmdDTO platformCmdDTO) {
        PlatformCmdDTO result = null;
        EProtocol eProtocol = EProtocol.getType(platformCmdDTO.getProtocolId());
        if(eProtocol == EProtocol.UNKNOW) {
            log.info(String.format("devId:{0} not find protocol",platformCmdDTO.getDevId()));
            return ;
        }
        EFrame tcpFrame = EFrame.getType(platformCmdDTO.getFrameId());
        if(tcpFrame == EFrame.unknow){
            log.info(String.format("devId:{0} not find frame",platformCmdDTO.getDevId()));
            return ;
        }
        ExchangeDTO exchangeDTO = iTransformService.platTransform(platformCmdDTO);
        if(exchangeDTO == null) {
            log.info(String.format("devId:{0} not translate cmd",platformCmdDTO.getDevId()));
            return ;
        }
        switch (eProtocol) {
            case TCP:{
                NotifyDeviceRegister.getInstance(tcpFrame).notifyMessage(exchangeDTO);
            }
            break;
            case MQTT:{

            }
            break;
            case UDP:{

            }
            break;
        }

    }




}
